//
//  WBMainViewController.swift
//  sinaWebo
//
//  Created by sinaFenQi on 2017/12/6.
//  Copyright © 2017年 sinaFenQi. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildControllers()
        setupcomletionBut()
    }
    /**
     portrait    : 竖屏，肖像
     landscape   : 横屏，风景画
     
     - 使用代码控制设备的方向，好处，可以在在需要横屏的时候，单独处理！
     - 设置支持的方向之后，当前的控制器及子控制器都会遵守这个方向！
     - 如果播放视频，通常是通过 modal 展现的！
     */

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    
    //private  能够保证方法私有，尽在本类调用此方法
    //objc   能够允许这个函数 在“运行时” 利用OC 机制调用
    @objc private func completionButClick() -> () {
        print("撰写微博")
    }
    

    private lazy var completionButton :UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
}
extension WBMainViewController {
    
    
    private func setupcomletionBut(){
        tabBar.addSubview(completionButton)
        
        let count = CGFloat(childViewControllers.count)
        let w = tabBar.bounds.width/count - 1
        completionButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        
        completionButton.addTarget(self, action: #selector(completionButClick), for: .touchUpInside)
    }
    
    //设置所有的子控制器
    private func setupChildControllers() {
        
        let array = [
            ["clsName":"WBHomeViewController","title":"首页","imageName":"home"],
            ["clsName":"WBMessageViewController","title":"消息","imageName":"message_center"],
            ["clsName":"UIViewController"],
            ["clsName":"WBDiscoverViewController","title":"发现","imageName":"discover"],
            ["clsName":"WBPodfileViewController","title":"我","imageName":"profile"],
        ]
        
        
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict as [String : AnyObject]))
        }
     viewControllers = arrayM
    }
    
    private func controller(dict: [String : AnyObject]) -> UIViewController {
        
        
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + (clsName as! String)) as? UIViewController.Type
        
            else {
            return  UIViewController()
        }
        
    
        let vc = cls.init()
        vc.title = title as? String ?? ""
        
        vc.tabBarItem.image = UIImage(named: "tabbar_" + (imageName as! String))
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + (imageName as! String) + "_selected")?.withRenderingMode(.alwaysOriginal)
        //MARK: 字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.orange], for: .highlighted)
        //MARK: 字体大小
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.font :UIFont.systemFont(ofSize: 12) ], for: .normal)
        let nav = WBNavigationController(rootViewController: vc)
        
        return nav
        
        
    }
}
